﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Blog.Core.Api.Filter
{
    /// <summary>
    /// 异常过滤器
    /// </summary>
    public class ExceptionFilter : IExceptionFilter
    {
        ////private readonly ILogger<ExceptionFilter> _logger;

        //public ExceptionFilter(ILogger<ExceptionFilter> logger)
        //{
        //    _logger = logger;
        //}
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {

            if (!context.ExceptionHandled)
            {
                //在方法中，日常，写入NLog日志
                var nlog = NLog.LogManager.GetCurrentClassLogger();

                //捕获到错误信息，把错误信息记录下来
                nlog.Error("捕获到错误：错误是" + context.Exception.Message);

                //_logger.LogError("捕捉到错误！错误内容为：" + context.Exception.Message);
                //throw new NotImplementedException();
            }

            context.ExceptionHandled = true;

        }
    }
}
